﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ServerYuGi;

namespace ReplayViewer
{
    public class TimeLine
    {
        public long Time;
        private bool is_start=false;
        List<LogReader.LogItem> Items;
        YUser p0,p1;

        int idx = 0;
        public TimeLine(LogReader log,SSupervisor sup)
        {
            Items=log.getMessageOfPlayer();
            p0 = sup.Players[0].MyConnection;
            p1 = sup.Players[1].MyConnection;
        }
        public bool IsRun { get { return is_start; } }
      
        public void Start()
        {
           
            Action<object> ac = (object o) =>
            {
                long step = 1;
                while (is_start)
                {

                    if (idx == Items.Count)
                        break;
                    if (Time > Items[idx].Time)
                    {
                        if (Items[idx].User_ID == p0.ID)
                            p0.SendFakeMessage(Items[idx].MSG_Header, Items[idx].getListArg());
                        else
                            p1.SendFakeMessage(Items[idx].MSG_Header, Items[idx].getListArg());
                        idx++;
                        Thread.Sleep(1000);
                    }
                    Time += step;
                    
                }
                // process Final
                Console.WriteLine("*****************************Simulation Complete***************************");
                Console.WriteLine("*****************************Simulation Complete***************************");
                Console.WriteLine("*****************************Simulation Complete***************************");
                Console.WriteLine("*****************************Simulation Complete***************************");
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(ac));
            is_start = true;
           
            Time = 0;
        }
        public void Stop()
        {
            is_start = false;
        }
    }
}
